using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Web.Profile;
using System.Xml.Serialization;
using Jaws.Core.Web.Data;

namespace Jaws.Core.Web.Entities.Profiles
{
    /// <summary>
    /// 
    /// </summary>
    [XmlRoot("profiles")]
    [Serializable]
    [XmlDataDirectory("profiles")]
    public class ProfileCollection : XmlCollectionBase<ProfileCollection,Profile>
    {
        private readonly object _lockObject = new object();
        private static ProfileCollection profiles = new ProfileCollection();
       
        /// <summary>
        /// Initializes a new instance of the <see cref="ProfileCollection"/> class.
        /// </summary>
        public ProfileCollection()
        {
        }

        internal ProfileCollection(IList<Profile> profiles) : base(profiles)
        {
        }

        /// <summary>
        /// Finds the name of the by user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="authenticationType">Type of the authentication.</param>
        /// <returns></returns>
        public Collection<Profile> FindByUserName(string userName, ProfileAuthenticationOption authenticationType)
        {
            var profilesTemp = new List<Profile>();

            foreach (Profile p in this)
            {
                if (string.Compare(userName, p.UserName, true, CultureInfo.CurrentCulture) == 0)
                {
                    switch (authenticationType)
                    {
                        case ProfileAuthenticationOption.All:
                            profilesTemp.Add(p);
                            break;
                        case ProfileAuthenticationOption.Anonymous:
                            if (p.IsAnonymous)
                            {
                                profilesTemp.Add(p);
                            }
                            break;
                        case ProfileAuthenticationOption.Authenticated:
                            if (!p.IsAnonymous)
                            {
                                profilesTemp.Add(p);
                            }
                            break;
                    }
                }
            }

            return new Collection<Profile>(profilesTemp);
        }

        /// <summary>
        /// Creates the profile info collection.
        /// </summary>
        /// <param name="authenticationOption">The authentication option.</param>
        /// <returns></returns>
        public ProfileInfoCollection CreateProfileInfoCollection(ProfileAuthenticationOption authenticationOption)
        {
            var pic = new ProfileInfoCollection();

            foreach (Profile p in this)
            {
                switch (authenticationOption)
                {
                    case ProfileAuthenticationOption.All:
                        pic.Add(p.CreateProfileInfo());
                        break;
                    case ProfileAuthenticationOption.Anonymous:
                        if (p.IsAnonymous)
                        {
                            pic.Add(p.CreateProfileInfo());
                        }
                        break;
                    case ProfileAuthenticationOption.Authenticated:
                        if (!p.IsAnonymous)
                        {
                            pic.Add(p.CreateProfileInfo());
                        }
                        break;
                }
            }

            return pic;
        }


    }
}